<template>
  <div>
    <div class="navbar">
      <span class="iconfont icon-caidan1" @click="showPopup"></span>
      <img src="/index/bgtop.png" @click="goIndex"/>
      <span class="iconfont icon-sousuo" @click="sousuo"></span>
      <div class="sreach" v-show="sos">
        <input type="text" v-model="sreach" @keyup.13="goSreach">
        <button @click="goSreach"><i class="iconfont icon-sousuo"></i></button>
      </div>
      <van-badge class="shop" :content="count" color="black">
        <span class="iconfont icon-gouwudai" @click="goShopcart"></span>
      </van-badge>
    </div>
    <van-popup v-model="show" :closeable="true" position="top" class="import">
      <div class="img">
        <img src="/index/bgtop.png" />
      </div>
      <div class="tabs">
        <ul>
          <li v-for="item in Type" :key="item.tid" @click="goProducts(item.tid)">{{item.name}}</li>
        </ul>
      </div>
      <a href="" class="pp">会员中心</a>
      <a href="" class="pp">品牌故事</a>
      <van-divider class="line" />
      <div class="user" @click="showRegLog" v-if="!isLogin">
        <span class="iconfont icon-yonghu"></span>
        <p>我的账户</p>
      </div>
      <div class="user" v-else @click="QuitLogin">
        <span class="iconfont icon-084tuichu"></span>
        <p>退出</p>
      </div>
    </van-popup>
  </div>
</template>
<script>
import Popup from "./Reglog.vue";
export default {
  components: { Popup },
    data() {
    return {
      count: 0,
      show: false,
      activeName: "",
      acName: "",
      Type:[],
      sreach:'',
      sos:false,
      isLogin:false
    };
  },
  mounted () {
    this.getTypes()
    this.setIslogin()
  },
  updated () {
    this.getCount() 
    this.upIslogin() 
  },
  methods: {
    showPopup() {
      this.show = true;
    },
    goIndex(){
      this.$router.push('/')
    },
    setIslogin(){
      sessionStorage.setItem('isLogin',this.isLogin)
    },
    upIslogin(){
      // console.log(this.isLogin);
      this.isLogin=this.$store.state.isLogin
    },
    showRegLog(){
      this.$router.push('/reglog')
      // sessionStorage.setItem('isLogin',this.isLogin)
    },
    QuitLogin(){
      let uid=null
      this.isLogin=false
      let shopcart=[]
      sessionStorage.removeItem('isLogin')
      sessionStorage.removeItem('uid',uid)
      this.$store.commit('getShopcart',shopcart)
      location.reload()
    },
    getTypes(){
      this.axios.get('/index/types').then(res=>{
        // console.log(res);
        this.Type=res.data.data
        let type=res.data.data
        this.$store.commit('getType',type)
      })
    },
    goProducts(id){
      this.$store.commit('getTid',id)
      this.$router.push('/products')
    },
    goSreach(){
      let sreac=this.sreach
      this.$router.push(`/sreach/${sreac}`)
    },
    sousuo(){
      if(this.sos){
        this.sos=false
      }else{
        this.sos=true
      }
    },
    goShopcart(){
      this.$router.push('/shopcart')
    },
    getCount(){
      let shopc=this.$store.state.shopcart
      let count = shopc.reduce((sum,item)=>{
        return sum + Number(item.count)
      },0)
      this.count=count
    }
  },
}
</script>
<style scoped lang="scss">
ul{
  list-style: none;
}
.navbar {
  // margin-top: 5px;
  height: 50px;
  border-bottom: 1px solid rgb(105, 105, 105);
  text-align: center;
  line-height: 50px;
  background-color: white;
  box-sizing: border-box;
  z-index: 99;
  position: fixed;
  top: 0;
  color: rgb(107, 107, 107);
  span:nth-child(1) {
    font-size: 1.8em;
    display: inline-block;
    position: absolute;
    bottom: 5px;
    left: 2px;
  }
  .sreach{
    width: 100%;
    height: 50px;
    line-height: 50px;
    position: relative;
    input{
      width:99%;
      height: 40px;
    }
    button{
      position: absolute;
      background-color: white;
      border: 0;
      top:4px;
      right:2px;
      line-height: 40px;
      height: 40px;
      i{
        font-size: 28px;
      }
    }
  }
  img {
    width: 40%;
  }
  span:nth-child(3) {
    font-size: 1.8em;
    position: absolute;
    right: 50px;
    bottom: 5px;
  }
  .shop {
    // font-size: 1.8em;
    position: absolute;
    bottom: 1px;
    right: 37px;
  }
}
.import {
  width:80%;
  height:90%;
  .img {
    width: 100%;
    height: 15%;
    margin-left: 30px;
    img {
      width: 60%;
      margin-top: 30px;
    }
  }
  a {
    color: #000;
    text-decoration: none;
    margin-left: 30px;
    display: block;
  }
  .tabs {
    margin: 20px 0 0 30px;
    ul{
      li{
        margin-top: 30px;
      }
    }
  }
  .pp {
    margin-top: 30px;
  }
  .line {
    margin-top: 30px;
    border-color: rgb(78, 78, 78);
  }
}
.user {
  position: absolute;
  bottom:60px;
  left:65px;
  width:50%;
  text-align: center;
  span{
    font-size: 3em;
  }
  p{
    font-size:20px;
  }
}
</style>
